import 'package:dio/dio.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_learn_vi/src/bean/bean_train.dart';
import 'package:flutter_learn_vi/src/bean/bean_user.dart';
import 'package:flutter_learn_vi/src/common/user_helper.dart';
import 'package:flutter_learn_vi/src/net/dio_utils.dart';
import 'package:flutter_learn_vi/src/page/common/common_loading_dialog.dart';
import 'package:flutter_learn_vi/src/page/common/common_nodata_widget.dart';
import 'package:flutter_learn_vi/src/page/train/train_list_item_widget.dart';
import 'package:flutter_learn_vi/src/utils/toast_utils.dart';

import '../../utils/navigator_utils.dart';

/// 创建人： Created by zhaolong
/// 创建时间：Created by  on 2021/1/14.
///
/// 可关注公众号：我的大前端生涯   获取最新技术分享
/// 可关注网易云课堂：https://study.163.com/instructor/1021406098.htm
/// 可关注博客：https://blog.csdn.net/zl18603543572
///
/// 代码清单
///培训列表
/// 教师与学员共用
class TrainItemPage extends StatefulWidget {
  final int pageFlag;

  TrainItemPage(this.pageFlag);

  @override
  State<StatefulWidget> createState() {
    return _TrainItemPageState();
  }
}

class _TrainItemPageState extends State<TrainItemPage>
    with AutomaticKeepAliveClientMixin {
  //标签页面保活 或者称保持状态
  @override
  bool get wantKeepAlive => true;

  @override
  void initState() {
    super.initState();
    print("页面创建 ${widget.pageFlag}");
    getMyTrainListFunction();
  }

  @override
  Widget build(BuildContext context) {
    super.build(context);

    if (_isLoading) {
      return LoadingWidget();
    }
    if (_list == null || _list.length == 0) {
      return NoDataWidget(
        clickCallBack: () async {
          setState(() {
            _isLoading = true;
          });
          await getMyTrainListFunction();
          ToastUtils.showToast("已刷新");
        },
      );
    }

    //一个列表 切记不要再使用 Scaffold
    //否则会拦截事件 失去滑动联动效果
    return RefreshIndicator(
      onRefresh: () async {
        await getMyTrainListFunction();
        ToastUtils.showToast("已刷新");
        return true;
      },
      child: ListView.builder(
        //去除内边距
        padding: EdgeInsets.all(0),
        //列表子Item个数
        itemCount: _list.length,
        //每个子Item的布局构建
        itemBuilder: (BuildContext context, int index) {
          TrainBean trainBean = _list[index];
          return TrainListItemWidget(trainBean: trainBean, index: index);
        },
      ),
    );
  }

  CancelToken _cancelToken;
  bool _isLoading = true;
  List<TrainBean> _list;

  Future<bool> getMyTrainListFunction() async {
    UserBean userBean = UserHelper.getInstance.userBean;
    Map<String, dynamic> monthMap = {
      "statues": widget.pageFlag,
      "department": UserHelper.getInstance.userBean.department,
      "userType": userBean.userType,
    };
    String url = HttpHelper.trainList;
    // if (userBean.userType == 12) {
    //   url = HttpHelper.trainTeacherCourseList;
    // }
    if (_cancelToken != null && !_cancelToken.isCancelled) {
      _cancelToken.cancel();
    }
    _cancelToken = new CancelToken();
    //答题数据
    ResponseInfo statisRresponseInfo = await DioUtils.instance.getRequest(
      url: url,
      queryParameters: monthMap,
      cancelTag: _cancelToken,
    );

    if (statisRresponseInfo.success) {
      List responseData = statisRresponseInfo.data["list"];
      if (responseData != null) {
        _list = [];
        responseData.forEach((element) {
          _list.add(TrainBean.fromJson(element));
        });
      }
    } else {
      if (statisRresponseInfo.code == 401) {
      } else {
        NavigatorUtils.pop(context);
      }
    }
    _isLoading = false;

    if (mounted) {
      setState(() {});
    }

    return Future.value(true);
  }
}
